<?php $this->load->view('templates/header');?>

<body>
<?php
/* INCLUSION OF LIBRARY FILEs*/
	require_once( 'lib/Facebook/FacebookSession.php');
	require_once( 'lib/Facebook/FacebookRequest.php' );
	require_once( 'lib/Facebook/FacebookResponse.php' );
	require_once( 'lib/Facebook/FacebookSDKException.php' );
	require_once( 'lib/Facebook/FacebookRequestException.php' );
	require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php');
	require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
	require_once( 'lib/Facebook/GraphObject.php' );
	require_once( 'lib/Facebook/GraphUser.php' );
	require_once( 'lib/Facebook/GraphSessionInfo.php' );
	require_once( 'lib/Facebook/Entities/AccessToken.php');
	require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
	require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php');
	require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php');
    
/* USE NAMESPACES */
	use Facebook\FacebookSession;
	use Facebook\FacebookRedirectLoginHelper;
	use Facebook\FacebookRequest;
	use Facebook\FacebookResponse;
	use Facebook\FacebookSDKException;
	use Facebook\FacebookRequestException;
	use Facebook\FacebookAuthorizationException;
	use Facebook\GraphObject;
	use Facebook\GraphUser;
	use Facebook\GraphSessionInfo;
	use Facebook\FacebookHttpable;
	use Facebook\FacebookCurlHttpClient;
	use Facebook\FacebookCurl;
    
/*PROCESS*/
	//1.Stat Session
	 session_start();
	//check if users wants to logout
	 if(isset($_REQUEST['logout'])){
	 	unset($_SESSION['fb_token']);
	 }
     
	//2.Use app id,secret and redirect url 
	$app_id = '465230510326848';
	$app_secret = '06e3a4e59d9e573da06757a451d146a7';
	$redirect_url='http://www.jessicahk.com/mywayofsitting/gallery/'; 
    
	//3.Initialize application, create helper object and get fb sess
	 FacebookSession::setDefaultApplication($app_id,$app_secret);
	 $helper = new FacebookRedirectLoginHelper($redirect_url);
	 $sess = $helper->getSessionFromRedirect();
     
	//check if facebook session exists
	if(isset($_SESSION['fb_token'])){
	 	$sess = new FacebookSession($_SESSION['fb_token']);
        try{
			$sess->Validate($id, $secret);
		}catch(FacebookAuthorizationException $e){
			print_r($e);
		}
	}
    
	$loggedin = false;
	//get email as well with user permission
	$login_url = $helper->getLoginUrl(array('email'));
	//logout
	$logout = 'http://eheadbeta.com/peydemo/?logout=true';
	//4. if fb sess exists echo name 
	 	if(isset($sess)){
	 	 try{
	 		//store the token in the php session
	 		$_SESSION['fb_token']=$sess->getToken();
	 		//create request object,execute and capture response
	 		$request = new FacebookRequest($sess,'GET','/me?fields=name,email');
			// from response get graph object
			$response = $request->execute();
            // Get the response typed as a GraphUser
			$graph = $response->getGraphObject(GraphUser::classname());
            //$graph_bd = $graph->backingData;
            // Get the base class GraphObject from the response
            $graph_obj = $response->getGraphObject();
            //print_r($graph_bd);
			// use graph object methods to get user details
			$id = $graph->getId();
			$name= $graph->getName();
			$email = $graph_obj->getProperty('email'); //$graph_obj->getProperty('email')
            if(isset($email)){
                $email = str_replace("@", "-", $email);
            } else {
                $email = 'none';
            }
            //$gender = $graph_obj->getProperty('gender');
			//$image = 'https://graph.facebook.com/'.$id.'/picture?width=300';
			$loggedin  = true;
       } catch(FacebookRequestException $e){
            echo $e->getMessage();
       }
	}
?>

    <!-- Navigation -->
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <div style="float:right">
                    <a class="navbar-brand" href="#">
                        <img src="http://www.jessicahk.com/mywayofsitting/webroot/images/logo-1.png" width="222px" height="54px">
                    </a>
                </div>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li>
                        <a href="<?php echo base_url()?>" class="current">比賽介紹</a>
                    </li>
                    <li>
                        <a href="<?php echo base_url()?>gallery">作品欣賞</a>
                    </li>
                    <li>
                        <a href="#">賽果公佈</a>
                    </li>
                    <li>
                        <a href="#">條款及細則</a>
                    </li>
                    <li class="header-upload-button">
                        <a href="<?php echo base_url()?>game">上載照片</a>
                    </li>
                    <li class="mobile-header-upload-button">
                        <a href="<?php echo base_url()?>game">上載照片</a>
                    </li>
                </ul>
                
            </div>

            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container -->
    </nav>

    <!-- Page Content -->
    <div class="main-container">
 
          <?php if($loggedin){ ?>
            <?php $this->load->helper('url');
            redirect('http://www.jessicahk.com/mywayofsitting/gallery/accessToken/' .$id .'/'. $name .'/'. $email)
            ?>
          <?php }else { ?>
            <?php $this->load->helper('url');
            redirect($redirect_url);?>
          <?php } ?>
          
        <!-- Footer -->
                <div class="footer-section col-lg-12">
                    <span class="footer-left" >Copyright &copy; 2015-16 Singapore Airlines. All Rights Reserved.</span>
                    <span class="footer-right" ><a href="javascript:void(0)">Privacy Policy</a> | <a href="javascript:void(0)">Terms and Conditions</a></span>
                </div>

    </div>
    <!-- /.container -->

<?php $this->load->view('templates/footer');?>
